//
//  LZBillboardViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "LZBillboardViewController.h"
#import "TabBarController.h"
#import "LZTableViewCell.h"
#import "FitHeader.h"
#import "TwoToOne.h"
#import "LZBillboardModel.h"
#import "LZFavouriteTableViewCell.h"
#import "LZPlayViewController.h"
#import "ZBBPlayHistory.h"
#import "S1mpl3AlbumViewController.h"
#import "S1mpl3HeadInfoViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "Model.h"
#import "UIImage+GIF.h"

@interface LZBillboardViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)TabBarController *tabBar;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSArray *array;
@property(nonatomic, strong)NSDictionary *sourceDic;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation LZBillboardViewController

#pragma mark - 视图将要出现时
- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
    [TabBarController shareInstance].myView.backgroundColor = [UIColor clearColor];
    [TabBarController shareInstance].tabBar.translucent = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
    [TabBarController shareInstance].tabBar.translucent = NO;
}
- (void)loadView{
    [super loadView];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 10 * FITHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    [[[_progressView subviews]firstObject] removeFromSuperview];
    [[[_progressView subviews] objectAtIndex:1] removeFromSuperview];
    [_progressView setMode:0];
    [_progressView show:YES];
    NSString  *name = @"RED.gif";
    _progressView.color = [UIColor clearColor];
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-70 * FITWidth, -80 * FITHeight, 140 * FITWidth, 100 * FITHeight)];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 10;
    imageView.opaque = NO;
    
    [[[[[_progressView subviews] lastObject] subviews] firstObject] removeFromSuperview];
    
    imageView.image = [UIImage sd_animatedGIFWithData:imageData];
    [[[_progressView subviews] lastObject] addSubview:imageView];
    [self.view addSubview:_progressView];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [_tableView.mj_header beginRefreshing];
    self.view.backgroundColor = [UIColor orangeColor];
    self.title = _bill.title;
    _dataSourceArray = [NSMutableArray array];
    _sourceDic = [NSDictionary dictionary];
    _page = 1;
    [self getData];
    
    if (!_fromZBB) {
        
        //下拉刷新
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataSourceArray removeAllObjects];
            _page = 1;
            [self getData];
        }];
        //上拉加载
        _tableView.mj_footer= [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page++;
            [self getData];
        }];
    }
    
}

- (void)getData{
    

    //NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=android&key=%@&pageId=1&pageSize=40", _bill.contentType, _bill.key];
    
    NSString *str = @"ranking%3Aalbum%3Aplayed%3A1%3A1";
    NSString *url = [NSString stringWithFormat:@" http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=%@%@&pageId=%ld&pageSize=40",str, _urlString, _page];

    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=android&key=%@&pageId=%ld&pageSize=40", _bill.contentType, _bill.key, _page];

    
    //如果来自朱彬彬页面,
    if (!_fromZBB) {
        _urlString = urlString;
    }
    else
    {
        _urlString = url;
    }
    [TwoToOne getDataByString:_urlString BodyString:nil WithDataBlock:^(id data) {
        NSArray *listArray = [data objectForKey:@"list"];
        _array = [data objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            LZBillboardModel *lzModel = [[LZBillboardModel alloc]init];
            [lzModel setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:lzModel];
        }
        [_tableView reloadData];
        if (!_fromZBB) {
            [_tableView.mj_footer endRefreshing];
            [_tableView.mj_header endRefreshing];
        }
        [_progressView removeFromSuperview];
       
    }];
    
    
}
#pragma mark - cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}
#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_section == 0) {
        if (_index == 0) {
            static NSString *identifier = @"cell";
            LZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[LZTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            if (_dataSourceArray.count == 0) {
                return cell;
            }
            cell.fromZBB = _fromZBB;
            cell.selectionStyle = 0;
            LZBillboardModel *lzModel = _dataSourceArray[indexPath.row];
            cell.lzModel = lzModel;
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
            cell.createdAtLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            if (indexPath.row == 0) {
                cell.createdAtLabel.textColor = [UIColor redColor];
            }else if (indexPath.row == 1){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.98 green:0.52 blue:0 alpha:1];
            }else if (indexPath.row == 2){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.59 green:0.69 blue:0 alpha:1];
            }
            return cell;
        }else{
            static NSString *identifier1 = @"cell1";
            LZFavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
            if (_dataSourceArray.count == 0) {
                return cell;
            }
            if (!cell) {
                cell = [[LZFavouriteTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
            }
            cell.selectionStyle = 0;
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
            LZBillboardModel *lzModel = _dataSourceArray[indexPath.row];
            cell.lzModel = lzModel;
            cell.createdAtLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
            if (indexPath.row == 0) {
                cell.createdAtLabel.textColor = [UIColor redColor];
            }else if (indexPath.row == 1){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.98 green:0.52 blue:0 alpha:1];
            }else if (indexPath.row == 2){
                cell.createdAtLabel.textColor = [UIColor colorWithRed:0.59 green:0.69 blue:0 alpha:1];
            }
            return cell;
        }
    }else{
        static NSString *identifier = @"cell";
        LZFavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (_dataSourceArray.count == 0) {
            return cell;
        }
        if (!cell) {
            cell = [[LZFavouriteTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = 0;
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        LZBillboardModel *lzModel = _dataSourceArray[indexPath.row];
        cell.lzModel1 = lzModel;
        cell.fromZBB = _fromZBB;
        cell.createdAtLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
        cell.createdAtLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        if (indexPath.row == 0) {
            cell.createdAtLabel.textColor = [UIColor redColor];
        }else if (indexPath.row == 1){
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.98 green:0.52 blue:0 alpha:1];
        }else if (indexPath.row == 2){
            cell.createdAtLabel.textColor = [UIColor colorWithRed:0.59 green:0.69 blue:0 alpha:1];
        }
        return cell;
    }
}
#pragma mark - 跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_fromZBB) {
        S1mpl3AlbumViewController *cs = [[S1mpl3AlbumViewController alloc]init];
        cs.albumId = [_dataSourceArray[indexPath.row] albumId];
        [self.navigationController pushViewController:cs animated:YES];
    }else{
        if (_section == 0) {
            if (_index == 0) {
                //            LZPlayViewController *lzPlay = [[LZPlayViewController alloc]init];
                [LZPlayViewController shareInstance];
                [LZPlayViewController shareInstance].index = indexPath.row;
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[LZPlayViewController shareInstance]];
                [LZPlayViewController shareInstance].modalTransitionStyle = 0;
                LZBillboardModel *lzBill = _dataSourceArray[indexPath.row];
                [LZPlayViewController shareInstance].lzBill = lzBill;
                
                [LZPlayViewController shareInstance].dataArray = _dataSourceArray;
                
                [LZPlayViewController shareInstance].dicArray = _array;
                NSDictionary *dic = _array[indexPath.row];
                [LZPlayViewController shareInstance].dic = dic;
                //            [[LZPlayViewController shareInstance] presentViewController:nav animated:YES completion:nil];
                [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cell0"];
                [self presentViewController:nav animated:YES completion:nil];
            }else {
                S1mpl3AlbumViewController *s1mpl3 = [[S1mpl3AlbumViewController alloc]init];
                LZBillboardModel *lzBill = _dataSourceArray [indexPath.row];
                s1mpl3.albumId = lzBill.albumId;
                s1mpl3.myTitle = lzBill.title;
                [self.navigationController pushViewController:s1mpl3 animated:YES];
            }
        }else{
            S1mpl3HeadInfoViewController *s1mpl3Head = [[S1mpl3HeadInfoViewController alloc]init];
            
            s1mpl3Head.s1mpl3HeadDataSourceDic = _array[indexPath.row];
            [self.navigationController pushViewController:s1mpl3Head animated:YES];
        }

    }
}
#pragma mark - cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_section == 0 && _index == 0) {
        return 80 * FITHeight;
    }
    return 90 * FITHeight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
